/** * A subclass of the Android ListView component that enables drag * and drop re-ordering of list items. * * Copyright 2012 Carl Bauer * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.mobeta.android.dslv; import android.graphics.Bitmap; import android.graphics.Point; import android.graphics.Color; import android.widget.ListView; import android.widget.ImageView; import android.view.View; import android.util.Log; /** * Simple implementation of the FloatViewManager class. Uses list * items as they appear in the ListView to create the floating View. */ public class SimpleFloatViewManager implements DragSortListView.FloatViewManager { private Bitmap mFloatBitmap; private ImageView mImageView; private int mFloatBGColor = Color.BLACK; protected ListView mListView; public SimpleFloatViewManager(ListView lv) { mListView = lv; } public void setBackgroundColor(int color) { mFloatBGColor = color; } /** * This simple implementation creates a Bitmap copy of the * list item currently shown at ListView <code>position</code>. */ @Override public View onCreateFloatView(int position) { // Guaranteed that this will not be null? I think so. Nope, got // a NullPointerException once... View v = mListView.getChildAt(position + mListView.getHeaderViewsCount() - mListView.getFirstVisiblePosition()); if (v == null) { return null; } v.setPressed(false); // Create a copy of the drawing cache so that it does not get // recycled by the framework when the list tries to clean up memory //v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); v.setDrawingCacheEnabled(true); mFloatBitmap = Bitmap.createBitmap(v.getDrawingCache()); v.setDrawingCacheEnabled(false); if (mImageView == null) { mImageView = new ImageView(mListView.getContext()); } mImageView.setBackgroundColor(mFloatBGColor); mImageView.setPadding(0, 0, 0, 0); mImageView.setImageBitmap(mFloatBitmap); return mImageView; } /** * This does nothing */ @Override public void onDragFloatView(View floatView, Point position, Point touch) { // do nothing } /** * Removes the Bitmap from the ImageView created in * onCreateFloatView() and tells the system to recycle it. */ @Override public void onDestroyFloatView(View floatView) { ((ImageView) floatView).setImageDrawable(null); mFloatBitmap.recycle(); mFloatBitmap = null; } }